package com.smart.ZC.State;

import java.io.IOException;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

public class GameMusicPlayer {

	private MediaPlayer[] BackGroudMusic;
	private HashMap<Integer, Integer> soundPoolMap;
	private SoundPool effectMusic;
	@SuppressWarnings("unused")
	private AudioManager audioManager;

	public GameMusicPlayer(Activity activity) {

		soundPoolMap = new HashMap<Integer, Integer>();
		audioManager = (AudioManager) activity
				.getSystemService(Context.AUDIO_SERVICE);
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
	}

	public void LoadBackGround(int[] id, Context context) {
		int num = id.length;
		BackGroudMusic = new MediaPlayer[num];
		for (int i = 0; i < num; i++) {
			BackGroudMusic[i] = MediaPlayer.create(context, id[i]);
			BackGroudMusic[i].setLooping(true);
		}

	}

	public void LoadBackGroundAssert(String[] pathFileName, Context context) {
		int num = pathFileName.length;

		BackGroudMusic = new MediaPlayer[num];
		for (int i = 0; i < num; i++) {
			try {
				AssetFileDescriptor afd = context.getResources().getAssets()
						.openFd(pathFileName[i]);
				BackGroudMusic[i] = new MediaPlayer();
				BackGroudMusic[i].setDataSource(afd.getFileDescriptor(),
						afd.getStartOffset(), afd.getLength());
				BackGroudMusic[i].prepare();
				BackGroudMusic[i].setLooping(true);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	public void LoadEffectMusic(int[] id, Context context) {
		int num = id.length;
		effectMusic = new SoundPool(num, AudioManager.STREAM_MUSIC, 100);
		for (int i = 0; i < num; i++) {
			soundPoolMap.put(i, effectMusic.load(context, id[i], 1));
		}
	}

	public void LoadEffectMusicAssertPath(String[] pathFileName, Context context) {
		effectMusic = new SoundPool(pathFileName.length,
				AudioManager.STREAM_MUSIC, 100);
		AssetFileDescriptor afd;
		for (int i = 0; i < pathFileName.length; i++) {
			try {
				afd = context.getResources().getAssets()
						.openFd(pathFileName[i]);
				soundPoolMap.put(i, effectMusic.load(afd, 1));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void playEffectMusic(int id) {// android:screenOrientation="landscape"
		effectMusic.play(soundPoolMap.get(id), 1, 1, 100, 0, 1);
	}

	public void playBgMusic(int id) {
		try {
			// BackGroudMusic[id].prepare();
			BackGroudMusic[id].start();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void stopToPlayBgMusic(int id) {
		try {
			BackGroudMusic[id].prepare();
			BackGroudMusic[id].start();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void pauseBgMusic(int id) {
		try {
			if (BackGroudMusic[id] != null) {
				BackGroudMusic[id].pause();
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void stopBgMusic(int id) {
		try {
			BackGroudMusic[id].stop();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int getBgMusicCurTime(int musicId) {
		int curTime = BackGroudMusic[musicId].getCurrentPosition();
		return curTime;
	}

	public MediaPlayer getBackGroudMusic(int id) {
		return BackGroudMusic[id];
	}

	// public void LoadBackGround(MediaPlayer[] BackGroudMusic,int[] id){
	//
	//
	// }

}
